<template>
  <div v-loading="delLoading">
    <div class="w-full min-h-0 bg-white dark:bg-regal-dark pl-5 pt-5 pr-5 rounded-lg" style="padding-bottom: 0">
      <el-form :inline="true" ref="ruleFormRef" :model="columnQuery">
        <el-form-item :label="item.label" :prop="item.column" v-for="item in config.query" :key="item.column" style="width: 250px">
          <el-input v-model="columnQuery[item.column]" :name="item.column" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchHandler">
            <Icon name="magnifying-glass" className="w-4 h-4 mr-1 -ml-1" />
            搜索
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">
            <Icon name="arrow-path" className="w-4 h-4 mr-1 -ml-1" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-default">
      <el-table :data="DataRes" class="mt-3" v-loading="loadingSelf" border>
        <el-table-column :prop="item.column" :label="item.label" v-for="item in config.title" :key="item.column" />
      </el-table>
      <div class="page-view" style="padding-top: 1rem; width: 100%; text-align: right">
        <el-pagination
          background
          layout="total,sizes,prev, pager,next"
          :current-page="pages.page"
          :page-size="pages.limit"
          @current-change="changePage"
          @size-change="changeLimit"
          :total="total"
          :page-sizes="[100, 500, 1000]"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, reactive, watch } from 'vue'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import { useRouter, useRoute } from 'vue-router'
import http from '/admin/support/http'
import type { FormInstance, FormRules } from 'element-plus'
const route = useRoute()

let ID = ref(route.path.substring(20))
let loadingSelf = ref(false)
const ruleFormRef = ref<FormInstance>()

let config = reactive({})
let columnQuery = reactive({})
let pages = reactive({
  page: 1,
  limit: 100,
})
let total = ref(0)

const getConfig = () => {
  if (ID.value) {
    http.get(`customReport/config/by/${ID.value}`).then(r => {
      let res = r.data.data
      config.query = res.query
      config.title = res.title
      console.log(config.query)
    })
  }
}

const columneHandler = () => {
  columnQuery = config.query.reduce((prev, item) => {
    prev[item.column] = null
    return prev
  }, {})
  return columnQuery
}

let DataRes = ref([])

const getData = () => {
  loadingSelf.value = true
  let sendObj = {
    ...columnQuery,
    ...pages,
  }
  http
    .get(`customReport/list/by/${ID.value}`, sendObj)
    .then(r => {
      total.value = r.data.total
      let res = r.data.data
      DataRes.value = res
      loadingSelf.value = false
    })
    .catch(error => {
      loadingSelf.value = false
    })
}

const searchHandler = () => {
  getData()
}

watch(
  route,
  (newVal, oldVal) => {
    // console.log('路径改变', newVal.path)
    ID.value = newVal.path.substring(20)
    if (ID.value && !isNaN(ID.value)) {
      getConfig()
      getData()
    }
  },
  { deep: true, immediate: true },
)

// onMounted(() => {
//   getData()
// })

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  for (let key in columnQuery) {
    columnQuery[key] = null
  }
  getData()
}

const changeLimit = e => {
  pages.limit = e
  getData()
}
const changePage = e => {
  pages.page = e
  getData()
}
</script>

<style lang="css" scoped>
.table-default {
  height: 70vh;
}
.el-table {
  height: 90%;
  margin-top: 0;
}
</style>
